<?php

/*
 * @Descripttion: 
 * @version: v1
 * @Author: 
 * @Date: 2022-02-15
 */

declare(strict_types=1);

namespace app\video\controller\admin\v1;

use think\Request;
use think\facade\Db;

class Aicam
{
    /**
     * @Descripttion:获取列表
     * @Author: 
     * @Date: 2022-02-15
     * @param {*}
     */
    public function getList(Request $request)
    {
        $data = $request->data;
        $limit =  $data['limit'] ?? 10;
        $page = $data['page'];
        $offset = ($page - 1) * $limit;
        $where = [
            ['ca.projectCode', '=', $request->prjCode],
            ['ca.module', '=', '05'],
        ];
        if (!empty($data['cameraType'])) array_push($where, ['tc.typeUuid', '=', $data['cameraType']]);
        if (!empty($data['cameraName'])) array_push($where, ['pc.name', 'like', '%' . $data['cameraName'] . '%']);
        if (!empty($data['alertType'])) array_push($where, ['ca.type', '=', $data['alertType']]);
        if (!empty($data['dateTime'])) array_push($where, ['ca.alertTime', 'between', $data['dateTime']]);

        $list = DB::table('video_cameraAlert')->alias('ca')
            ->join('video_projectCamera pc', 'ca.cameraUuid  = pc.uuid')
            ->join('video_typeCamera tc', 'tc.cameraUuid  = pc.uuid','left')
            ->join('video_type t', 't.uuid  = tc.typeUuid','left')
            ->where($where)
            ->field(['ca.uuid', 'pc.name as cameraName', 't.name as cameraType', 'ca.type as alertType', 'ca.message', 'ca.alertTime', 'ca.image as imageUrl', 'ca.video as videoUrl'])
            ->limit((int)$offset, (int)$limit)
            ->group('ca.id')
            ->order('ca.id', 'DESC')
            ->select()->toArray();

        $count = DB::table('video_cameraAlert')->alias('ca')
            ->join('video_projectCamera pc', 'ca.cameraUuid  = pc.uuid')
            ->join('video_typeCamera tc', 'tc.cameraUuid  = pc.uuid','left')
            ->join('video_type t', 't.uuid  = tc.typeUuid','left')
            ->group('ca.id')
            ->where($where)
            ->count();
        $data = [
            'list' => $list,
            'count' => $count,
        ];

        return result(SUCCESS, $data, '');
    }

    /**
     * @Descripttion:AI摄像头预警字典表
     * @Author: 
     * @Date: 2022-02-15
     * @param {*}
     */
    public function getOptions(Request $request)
    {
        $data = $request->data;
        $where = [
           
        ];

        $cameraType = DB::table('video_type')
            ->where($where)
            ->field(['uuid', 'name'])
            ->order('id', 'DESC')
            ->select()->toArray();

        $type = DB::table('dicaiAlertType')
            ->where($where)
            ->field(['zhCnName as type', 'enName as code'])
            ->order('id', 'DESC')
            ->select()->toArray();
        $data = [
            'cameraType' => $cameraType,
            'type' => $type,
        ];

        return result(SUCCESS, $data, '');
    }

    public function destroy(Request $request)
    {
        $data = $request->data;
        $where = [
            ['projectCode', '=', $request->prjCode],
            ['uuid', 'in', $data['uuid']]
        ];
        $checkData = DB::table('video_cameraAlert')->where($where)->delete();
        if ($checkData) return result(SUCCESS, '', '删除成功');
        return result(FAIL, '', '删除失败');
    }
}
